package com.mobilesales.android.activities.orders;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;

import com.mobilesales.android.R;
import com.mobilesales.android.activities.ListModelActivity;
import com.mobilesales.android.adapters.OrderAdapter;
import com.mobilesales.android.adapters.SlidingMenus;
import com.mobilesales.android.database.ClientManager;
import com.mobilesales.android.database.OrderManager;
import com.mobilesales.android.infrastructure.SyncItemTask;
import com.mobilesales.android.model.clients.Client;
import com.mobilesales.android.model.orders.Order;
import com.mobilesales.android.util.Constants;
import com.mobilesales.android.util.FilterFieldWatcher;
import com.mobilesales.android.util.MessageBox;

public class Orders extends ListModelActivity<Order> {

	private EditText searchOrderField;
	private OrderAdapter itemsAdapter;
	private FilterFieldWatcher filter;
	private List<Client> clients;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.orders);
		new SlidingMenus(this);
		this.searchOrderField = ((EditText) findViewById(R.id.orderFilterText));
		loadItems();
	}

	private void loadItems() {
		clients = this.getClients();
		if (clients != null) {
			List<Order> orders = this.getItems();
			if (orders != null) {
				this.setItemsSource(orders);
			} else {
				MessageBox.show(this, getString(R.string.ErrorOrderList));
			}
		} else {
			MessageBox.show(this, getString(R.string.ErrorClientList));
		}
	}

	@Override
	protected void onListItemClick(ListView listView, View view, int position,
			long id) {
		super.onListItemClick(listView, view, position, id);
		Order order = (Order) this.itemsAdapter.getItem(position);
		Intent intent = new Intent(this, StoreOrder.class);
		intent.putExtra(Constants.OrderId, order.Id);
		startActivityForResult(intent, 0);
	}

	private List<Client> getClients() {
		ClientManager manager = new ClientManager(this);
		List<Client> result = manager.GetClientList();
		manager.Close();
		return result;
	}

	public void refresh() {
		this.getHandler().post(new Runnable() {
			public void run() {
				loadItems();
			}
		});
	}

	@Override
	protected void setItemsSource(List<Order> source) {
		this.itemsAdapter = new OrderAdapter(this, R.layout.order_list_item,
				source, clients);
		this.setListAdapter(itemsAdapter);
		this.filter = new FilterFieldWatcher(this.itemsAdapter);
		this.searchOrderField.addTextChangedListener(this.filter);
	}

	@Override
	protected List<Order> getItems() {
		OrderManager manager = new OrderManager(this);
		List<Order> result = manager.GetOrderList();
		manager.Close();
		return result;
	}

	public void createOrder(View v) {
		this.startActivityForResult(new Intent(this, StoreOrder.class), 0);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (this.filter != null)
			this.searchOrderField.removeTextChangedListener(this.filter);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == Activity.RESULT_OK) {
			SyncItemTask sync = new SyncItemTask(this, 1);
			sync.execute();
		}
		loadItems();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater menuinf = new MenuInflater(this);
		menuinf.inflate(R.menu.order_menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		int i = item.getItemId();
		switch (i) {
		case R.id.menu_order_newOrder:
			createOrder(null);
			break;
		default:
			break;
		}
		return true;
	}
}
